<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加续签合同</title>
<link rel="stylesheet" href="/js/plugin/layui-v2.3.0/layui/css/layui.css">
<link rel="stylesheet" href="/css/reset.css">
<link rel="stylesheet" href="/css/index.css">
<link rel="stylesheet" href="/css/project_set.css">
<script src="/js/jquery-2.1.1.min.js"></script>
<script src="/js/plugin/layui-v2.3.0/layui/layui.js"></script>
<script src="/js/common.js"></script>
</head>
<style>
::-webkit-scrollbar-thumb {
	background: #a2a2a2;
	border-radius: 10px;
	height: 50%;
}

::-webkit-scrollbar {
	width: 6px;
}

body {
	overflow-y: scroll;
}
.error-info{color: #E03641;}

.block {
	display: block;
}
</style>
<body>
	<div class="kb-content">
		<section class="kb-content-container clearfix">
			<form lay-filter="contractInfo" class="layui-form">
				<input type="hidden" id="projectId" name="projectId" th:value="${project.id}" />
				<input type="hidden" id="fkTeam" name="fkTeam" th:value="${fkTeam}" />
				<input type="hidden" id="renewid" name="renewid" th:value="${c.id}" />
				<input type="hidden" id="month" name="month" th:value="${c.depositMonth}" />
				<input type="hidden" id="ciEndDate" name="ciEndDate" th:value="${#dates.format(c.realEndDate,'yyyy-MM-dd')}" />
				<div class="kb-title" style="margin-left: 30px; margin-right: 30px; padding-top: 30px;">
					<span class="header-title">添加续签合同</span>
				</div>
				<div style="width: 100%; height: 100%; position: relative;">
					<div class="kb-form-container">
						<fieldset class="fieldset">
							<legend>甲方信息</legend>
							<div class="location-provider">
								<div class="provider-line flex-between">
									<div>
										<span>名称</span>
										<span th:text="${project.partyName}"></span>
									</div>

								</div>
								<div class="provider-line flex-between">
									<div>
										<span>开户行</span>
										<span th:text="${project.partyOpening}"></span>
									</div>
									<div>
										<span>地址</span>
										<span th:text="${project.partyAddress}"></span>
									</div>
								</div>
								<div class="provider-line flex-between">
									<div>
										<span>账号</span>
										<span th:text="${project.partyAccount}"></span>
									</div>
									<div>
										<span>联系电话</span>
										<span th:text="${project.partyTelephone}"></span>
									</div>
								</div>
							</div>
						</fieldset>
						<fieldset class="fieldset">
							<legend>合同日期</legend>
							<div class="layui-form-item">
								<div class="layui-col-sm6 layui-col-md6">
									<label class="layui-form-label">签约日期</label>
									<div class="layui-input-block">
										<input type="text" name="signTime" id="signTime" lay-verify="signTime" autocomplete="off" class="layui-input">
										<span style="color: rgb(153, 153, 153); font-size: 12px; padding: 4px 0px 0px;">签订合同的日期</span>
									</div>
								</div>

							</div>
							<div class="layui-form-item layui-row ">
								<div class="layui-col-sm6 layui-col-md6">
									<label class="layui-form-label"><span class="must-fill">*</span>起始日期</label>
									<div class="layui-input-block">
										<input type="text" name="startDate" id="startDate" lay-verify="startDate" autocomplete="off" class="layui-input" >
										<span style="color: rgb(153, 153, 153); font-size: 12px; padding: 4px 0px 0px;">合同开始生效日期</span>
									</div>
								</div>
								<div class="layui-col-sm6 layui-col-md6">
									<label class="layui-form-label t-right"><span class="must-fill">*</span>终止日期</label>
									<div class="layui-input-block">
										<input type="text" name="endDate" id="endDate" lay-verify="endDate" autocomplete="off" class="layui-input">
										<span style="color: rgb(153, 153, 153); font-size: 12px; padding: 4px 0px 0px;">合同终止日期</span>
										<div id="date-btn">
											<span class="layui-btn  layui-btn-sm layui-btn-normal mag-bot-20" data-type="0">一个月</span>
											<span class="layui-btn  layui-btn-sm layui-btn-normal mag-bot-20" data-type="1">三个月</span>
											<span class="layui-btn  layui-btn-sm layui-btn-normal mag-bot-20" data-type="2">六个月</span>
											<span class="layui-btn  layui-btn-sm layui-btn-normal mag-bot-20" data-type="3">一年</span>
										</div>
									</div>
								</div>
							</div>

						</fieldset>

						<fieldset class="fieldset">
							<legend>工位设置</legend>
							<div>
								<div style="padding-left: 30px;" id="kaifang">
									<legend style="font-size: 16px;">开放工位</legend>
									<ul class="region-input-box" id="kaifangCont">

									</ul>
								</div>
								<div style="padding-left: 30px;" id="fengbi">
									<legend style="font-size: 16px;">封闭工位</legend>
									<ul class="region-input-box" id="fengbiCont">

									</ul>
								</div>
							</div>
							<div>
								<a class="layui-btn layui-btn-radius layui-btn-normal mag-bot-20" onclick="addPartition(2)">
									<i class="layui-icon">&#xe654;</i>添加封闭工位
								</a>
								<a class="layui-btn layui-btn-radius layui-btn-normal mag-bot-20" onclick="addPartition(1)">
									<i class="layui-icon">&#xe654;</i>添加开放工位
								</a>
							</div>
							<p class="error-info fq-erro"></p>
						</fieldset>
						<fieldset class="fieldset">
							<legend>押金信息</legend>
							<div class="layui-form-item layui-row ">
								<div class="layui-col-sm6 layui-col-md6">
									<label class="layui-form-label">押金</label>
									<div class="layui-input-block">
										<select lay-filter="yajin" name="interest">
											<option value="1" selected="">一个月</option>
											<option value="2">二个月</option>
											<option value="3">三个月</option>
											<option value="4">四个月</option>
											<option value="5">五个月</option>
											<option value="6">六个月</option>
											<option value="7">七个月</option>
											<option value="8">八个月</option>
											<option value="9">九个月</option>
											<option value="10">十个月</option>
											<option value="11">十一个月</option>
											<option value="12">十二个月</option>
										</select>
									</div>
								</div>
								<div class="layui-col-sm6 layui-col-md6">
									<label class="layui-form-label t-right"><span class="must-fill">*</span>押金总金额</label>
									<div class="layui-input-block">
										<input type="text" name="deposit" id="deposit" placeholder="" autocomplete="off" class="layui-input" th:value="${c.deposit}">
									</div>
								</div>
							</div>
						</fieldset>
						<fieldset class="fieldset">
							<legend>
								缴租阶段
								<span style="color: rgb(153, 153, 153); font-size: 12px; padding: 4px 0px 0px;">(更改工位设置后请重新生成缴租阶段)</span>
							</legend>
							<p class='mag-bot-20'>缴租阶段是指使用同样的缴租规则的一个或多个缴租周期。例如，如果所有月份缴租方式和金额都是一样的话，就可以创建一个缴租阶段。如果前两个月免费，后八个月正常付款，则可以创建两个缴租阶段。每个缴租阶段可以具体规定每个周期长度和金额。所有缴租阶段必须连续而且覆盖租约的整个时间段。</p>
							<div style="padding-left: 20px;" class="form-content">
								<div class="mag-top-20" id="receivable"></div>
								<div class="total-num"></div>
								<div class="mag-bot-20">
									<a class="layui-btn layui-btn-radius layui-btn-normal " id="addReceivable">
										<i class="layui-icon">&#xe654;</i>添加缴租阶段
									</a>
								</div>
								<p class="error-info jiaozu-erro"></p>
							</div>
						</fieldset>
						<fieldset class="fieldset">
							<legend>合同编号</legend>
							<div class="layui-form-item">
								<label class="layui-form-label">合同编号</label>
								<div class="layui-input-block">
									<input type="text" name="contractNumber" id="contractNumber" placeholder="" autocomplete="off" class="layui-input"  lay-verify="contractNumber">
								</div>
							</div>
						</fieldset>
						<fieldset class="fieldset">
							<legend>合同备注</legend>
							<div class="layui-form-item">
								<label class="layui-form-label">营业执照号</label>
								<div class="layui-input-block">
									<textarea name="remark" id="remark" placeholder="备注" class="layui-textarea"></textarea>
									<span style="color: rgb(153, 153, 153); font-size: 12px; padding: 4px 0px 0px;">备注信息只有内部员工可见</span>
								</div>
							</div>
						</fieldset>
						<fieldset class="fieldset">
							<legend>补充协议</legend>
							<div class="layui-form-item">
								<button class="layui-btn layui-btn-normal">上传补充协议</button>
							</div>
						</fieldset>
						<div>
							<div class="f-right">
								<button class="layui-btn layui-btn-normal btn-bg mag-bot-20 " lay-submit lay-filter="ccc">保存</button>
							</div>
						</div>
					</div>
				</div>
				<input type="hidden" id="id" name="id" th:value="${id}" />
			</form>
		</section>
	</div>
</body>
<script>

var allPartitionId="";
var dateArr = $("#ciEndDate").val().split("-");
dateArr[2]++;
var date = dateArr.join("-");
var dateObj='', type=0;
$(function(){
	renderForm();
	$("#kaifang").hide();
	$("#fengbi").hide();
	getnow();
	setHeight()
	layui.config({
	        base: '/js/plugin/layui-v2.3.0/layui/' //此处路径请自行处理, 可以使用绝对路径
	    }).extend({
	        formSelects: 'formSelects-v3'
	    }).use(['element',"layer","form",'formSelects', 'laydate'], function(){
			var layer=layui.layer,formSelects = layui.formSelects,laydate = layui.laydate;form = layui.form;
			
			//日期
			laydate.render({
			  elem: '#signTime'
			});
			laydate.render({
				  elem: '#startDate'
				  ,value:date
				  ,done: function(value, date, endDate){
					  dateObj=value;
					  renderEndDate(dateObj,type);
					    //console.log(value); //得到日期生成的值，如：2017-08-18
					    //console.log(date); //得到日期时间对象：{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}
					   // console.log(endDate); //得结束的日期时间对象，开启范围选择（range: true）才会返回。对象成员同上。
				   }
				});
			laydate.render({
				  elem: '#endDate'
				});
			form.on('select(yajin)', function(data){
				 // console.log(data.value);
				 // $("#deposit").val($("#deposit").val()*data.value);
				  yjSum();
				});
			form.val('contractInfo', {'interest':$("#month").val()});
			form.verify({
				contractNumber: function(value, item){ //value：表单的值、item：表单的DOM对象
					if(value != ""){
						var flag = true;
						$.ajax({
				             type: "post",
				             url: "/app/contractInfo/checkContractNum",
				             data: {contractNum:value},
				             dataType: "json",
				             async:false,//同步请求
				             success: function(data){
				            	 if(data > 0){
										flag = false;
									}
				              }
				         });
						if(flag == false){
							return "合同编号不能重复";
						}
						
					}
				  } 
				  //我们既支持上述函数式的方式，也支持下述数组的形式
				  //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
				  
				}); 
			//监听提交
			  form.on('submit(ccc)', function(data){
				  var flag = true;
				  $(".error-info").html("");//错误提示清空
				  var kf = $("#kaifangCont li");
				  var gwList = "";
				  for(var i = 0;i < kf.length;i++){
					  var id = $(kf[i]).attr("id").substring(0,$(kf[i]).attr("id").length-2);
					  var aInp = $(kf[i]).find("input");
					  var stationPrice = aInp[0].value;
					  var stationNumber = aInp[1].value;
					  var totalRent = aInp[2].value;
					  if(stationNumber > $(aInp[1]).attr("data-num")){
						  $(aInp[1]).next().html("所选分区工位数不足");
						  flag = false;
					  }
					  if(!checkNum(stationPrice)){
						  $(aInp[0]).next().html("请输入正确的价格, 小数点保留2位");
						  flag = false;
					  }
					  if($(kf[i]).find(".fkStation").val() == ""){
						  layer.msg("开发分区请手动选择工位");
						  flag = false;
					  }else if($(kf[i]).find(".fkStation").val().split(",").length -1 != stationNumber){
						  layer.msg("开发分区已选工位跟工位个数不符");
						  flag = false;
					  }
					  gwList += ",{\"id\":\""+id+"\",\"stationPrice\":"+stationPrice+",\"stationNumber\":"+stationNumber
						  +",\"roomPrice\":0,\"totalRent\":"+totalRent+"}";
				  }
				  var fb = $("#fengbiCont li");
				  for(var i = 0;i < fb.length;i++){
					  var id = $(fb[i]).attr("id").substring(0,$(fb[i]).attr("id").length-2);
					  var aInp = $(fb[i]).find("input");
					  var stationPrice = aInp[0].value;
					  var stationNumber = aInp[1].value;
					  var roomPrice = aInp[2].value;
					  var totalRent = aInp[3].value;
					  if(stationNumber > $(aInp[1]).attr("data-num")){
						  $(aInp[1]).next().html("所选分区工位数不足");
						  flag = false;
					  }
					  if(!checkNum(stationPrice)){
						  $(aInp[0]).next().html("请输入正确的价格, 小数点保留2位");
						  flag = false;
					  }
					  gwList += ",{\"id\":\""+id+"\",\"stationPrice\":"+stationPrice+",\"stationNumber\":"+stationNumber
					  +",\"roomPrice\":"+roomPrice+",\"totalRent\":"+totalRent+"}";
				  }
				  if(gwList != ""){
					  gwList = gwList.substring(1);
					  gwList = "["+gwList+"]";
				  }else{
						 $(".fq-erro").html("至少选择一个工位");
						 flag = false;
					  }
				  
				  var ys = $("#receivable tr");
				  var ysList = "";
				  for(var i = 0;i<ys.length;i++){
					  var startdate = $(ys[i]).find("input[name='startdate']").val();
					  var enddate = $(ys[i]).find("input[name='enddate']").val();
					  if(i == 0){
						  if(startdate != $("#startDate").val()){
							  $(".jiaozu-erro").html("合同开始时间必须等于合同周期首'阶段开始'日期");
							  flag = false;
						  }
					  }else{
						  var end = $(ys[i-1]).find("input[name='enddate']").val().split("-");
						  
						  end[2]++;
						  if(end[2]<10){
							  end[2] = "0"+end[2];
						  }
						  var endNew = end.join("-");
						  if(startdate != endNew){
							  $(ys[i]).find("p").html( "缴租阶段结束时间和开始时间必须连续");
							  flag = false;
						  }
						  if(i == ys.length-1){
							  if(enddate != $("#endDate").val()){
								  $(".jiaozu-erro").html("合同结束时间必须等于合同周期最后'阶段终止'日期");
								  flag = false;
							  }
						  }
					  }
					  var receivableDate = $(ys[i]).find("input[name='receivableDate']").val();
					  var receivableAmount = $(ys[i]).find("input[name='price_min']").val();
					  var  id = $(ys[i]).attr("data-id");
					  ysList += ",{\"id\":\""+id+"\",\"startDate\":\""+startdate+"\",\"endDate\":\""+enddate
					  +"\",\"receivableDate\":\""+receivableDate+"\",\"receivableAmount\":"+receivableAmount+"}";
				  }
				  if(ysList != ""){
					  ysList = ysList.substring(1);
					  ysList = "["+ysList+"]";
				  }else{
					  $(".jiaozu-erro").html("请添加至少一个缴租阶段");
					  flag = false;
				  }
				  var fkProject = $("#projectId").val();
				  var signTime = $("#signTime").val();
				  var startDate = $("#startDate").val();
				  var endDate = $("#endDate").val();
				  var depositMonth = $("select[name='interest']").val();
				  var deposit = $("#deposit").val();
				  var contractNumber = $("#contractNumber").val();
				  var remark = $("#remark").val();
				  var id=$("#id").val();
				  var fkTeam=$("#fkTeam").val();
				  var renewid=$("#renewid").val();
				 if(flag == true){
				  jQuery.ajax({
				        type:"post",
				        async:false,
				        url:"/app/contractInfo/save",
				        dataType:"json",
				        data:{
							  id:id,
							  fkTeam:fkTeam,
							  fkProject:fkProject,
							  signTime:signTime,
							  startDate:startDate,
							  endDate:endDate,
							  realEndDate:endDate,
							  depositMonth:depositMonth,
							  deposit:deposit,
							  contractNumber:contractNumber,
							  remark:remark,
							  renewid:renewid,
							  ysList:ysList,
							  gwList:gwList
					  },
					  success:function(result){
						  layer.msg(result.msg);
						  var id=$("#id").val();
						  var url = '/app/contractInfo/detailCenter/' + id;
						  var iframe = window.parent.document.getElementById("project-iframe");
						  if(!iframe){
								iframe = window.parent.document.getElementById('iframe-top');
							}
						    iframe.src = url;
						 
						  }
					  })
				 }
			    return false; 
			  });

			  $("#date-btn").on("click","span",function(){
				  type=$(this).attr("data-type");
				  dateObj = $("#startDate").val();
				  renderEndDate(dateObj,type)
			  })
			  $("#addReceivable").on("click",function(){
				  var endDate = $("#endDate").val();
				  if(endDate == ""){
					layer.msg("请选择终止日期！");
				 }else{
				  	var id=$("#id").val();
					top.layer.open({
						title : [ '添加缴租阶段'],
						type : 2,
						area : ['650px','500px'],
						shade : [0.6, '#010101'],
						content : [ "/app/contractReceivable/add/"+id],
						end: function () {
							getReceivable();
					    }
					});
				 }
			  })
			  
			  //自定义时间
			  function renderEndDate(data,type){
				  var di = data.split("-");
				  var y=di[0],m=di[1],d=di[2],value='';
					  if(type==0){
						  m=parseInt(m)+1;
					  }else if(type==1){
						 m=parseInt(m)+3;
					  }else if(type==2){
						 m=parseInt(m)+6;
					  }else if(type==3){
						 y=parseInt(y)+1;
					  }
					  d-=1;		
				  var now= new Date(y,m,d),endY=now.getFullYear(),endM=now.getMonth(),endD=now.getDate();
				  value=endY+"-"+endM+"-"+endD;
				  laydate.render({
					  elem: '#endDate'
					  ,value: value
				  });
			  }
			  getPartition();//获取工位	
	});
	//监听选择工位
	$('.region-input-box').on("click","input[name='radio-0']",function(){
		var flegC=$(this).prop("checked"),val=$(this).val();
		if(flegC&&val==1){
			$(this).parents('.total-rent').find('.xiugaiBtn').removeClass("layui-btn-disabled").addClass("layui-btn-normal");
			var gwCount = $(this).parent().parent().parent().parent().parent().find("input").eq(1).val();
			var stationId = $(this).siblings().find(".fkStation").val();
			manualAdd($(this).attr("data-id"),gwCount,stationId);
		}else{
			$(this).parents('.total-rent').find('.xiugaiBtn').addClass("layui-btn-disabled").removeClass("layui-btn-normal")
			
		}
	})
	
	
})
function xiugai(obj){
	var gwCount = $(obj).parent().parent().parent().parent().parent().find("input").eq(1).val();
	var stationId = $(obj).siblings().find(".fkStation").val();
	var dataId = $(obj).parent().find("input[name='radio-0']").attr("data-id");
	manualAdd(dataId,gwCount,stationId);
}
 function setHeight(){
     /* var h = document.body.clientHeight*1;
    window.parent.parentHeight(h); */
    
}
function addPartition(type){
	var projectId = $("#projectId").val();
	var endDate =$("#endDate").val();
	var fengbiIds = "";
	var fengbi = $("#fengbiCont li");
	for(var i = 0;i < fengbi.length;i++){
		fengbiIds += $(fengbi[i]).attr("pId")+",";
	}
	if(endDate == ""){
		layer.msg("请选择终止日期！");
	}else{
		var id=$("#id").val();
		var date = $("#startDate").val();
		parent.parent.parent.layer.open({
			title : [ '选择分区'],
			type : 2,
			area : ['630px','80%'],
			shade : [0.6, '#010101'],
			content : [ "/app/projectPartition/radio/"+type+"_"+id+"_"+date+"_"+fengbiIds+"_"+projectId],
			end: function () {
				getPartition();
		    }
		});
	}
	 
	
}
//手动分配工位
function manualAdd(id,gwCount,stationId){
	var contractId=$("#id").val();
	parent.parent.parent.layer.open({
		title : [ '选择工位'],
		type : 2,
		area : ['630px','80%'],
		shade : [0.6, '#010101'],
		content : [ "/app/projectStation/manualAdd/"+id+"_"+gwCount+"_"+stationId+"_"+contractId],
		end: function () {
			getStation(id);
	    }
	}); 
	
}
//获取station列表
function getStation(partitionId){
	var id=$("#id").val();
	var url="/app/contractStation/listdata";
	jQuery.ajax({
        type:"post",
        async:false,
        url:url,
        dataType:"json",
        data:{'params[contractId]':id,'params[partitionId]':partitionId},
        success:function(data){
        	var data = data.data;
        	var name="";
        	var sId = "";
        	for(var i = 0;i<data.length;i++){
        		name += data[i].station.stationName+",";
        		sId += data[i].station.id +",";
        	}
        	var opt = $("[data-id='"+partitionId+"']").parent();
        	$(opt).find(".yifenpei").css("display","inline-block");
        	$(opt).find(".xiugaiBtn").removeClass("layui-btn-disabled").addClass("layui-btn-normal");
        	$(opt).find(".stationName").text(name);
        	$(opt).find(".fkStation").val(sId);
        }
    })
}

function getReceivable(){
	var id=$("#id").val();
	var url="/app/contractReceivable/listdata";
	jQuery.ajax({
        type:"post",
        async:false,
        url:url,
        dataType:"json",
        data:{'params[contractId]':id},
        success:function(data){
        	receivable(data.data);
        }
    })
}
function receivable(data){
	var cont='<div style="padding-left: 15px;"><table class="content-table"><tbody>';
	var startDate="";
	var endDate="";
	var he=0;
	for (var i = 0; i < data.length; i++) {
		if(i==0){
			startDate=data[i].startDate;
		}
		if(i==data.length-1){
			endDate=data[i].endDate;
		}
		he+=data[i].receivableAmount*1;
		cont+=`<tr data-id="`+data[i].id+`">`
				+`<td>`
					+`<div class="layui-form-item">`
					    +`<div class="layui-inline">`
					      +`<label class="layui-form-label">日期</label>`
					      +`<div class="layui-input-inline" style="width: 160px;">`
					        +`<input type="text" name="startdate" id="startdate`+i+`" value="`+data[i].startDate+`" placeholder="" autocomplete="off" class="layui-input">`
					        +`<p class="error-info ysdate"></p>`
					        +`</div>`
					      +`<div class="layui-form-mid">-</div>`
					      +`<div class="layui-input-inline" style="width: 160px;">`
					        +`<input type="text" name="enddate" id="enddate`+i+`" value="`+data[i].endDate+`" placeholder="" autocomplete="off" class="layui-input">`
					      +`</div>`
					    +`</div>`
					+`</div>`
				+`</td>`
				+`<td>`
					+`<div class="layui-form-item">`
					    +`<div class="layui-inline">`
					      +`<label class="layui-form-label">租期日</label>`
					      +`<div class="layui-input-inline" style="width: 160px;">`
					        +`<input type="text" name="receivableDate" value="`+data[i].receivableDate+`" placeholder="" autocomplete="off" class="layui-input">`
					      +`</div>`
					    +`</div>`
					+`</div>`
				+`</td>`
				+`<td>`
					+`<div class="layui-form-item">`
					    +`<div class="layui-inline">`
					      +`<label class="layui-form-label">总金额</label>`
					      +`<div class="layui-input-inline" style="width: 160px;">`
					        +`<input type="text" name="price_min" value="`+data[i].receivableAmount+`" placeholder="" autocomplete="off" class="layui-input">`
					      +`</div>`
					    +`</div>`
					+`</div>`
				+`</td>`
				+`<td>`
					+`<i class="layui-icon layui-icon-close a-hover"  onclick=delReceivable("`+data[i].id+`")></i>`
				+`</td>`
			+`</tr>`;
		
	}
	cont+='</tbody></table></div>';
	var cont1=`<div class="flex-between" style="padding-bottom: 10px; border-bottom: 1px solid rgb(229, 229, 229)">`
					+`<div>`
					+`<span style="margin-right: 15px; font-size: 16px; color: rgb(51, 51, 51);">`+startDate+` - `+endDate+`</span>`
					+`<span style="font-size: 10px;">(缴租阶段)</span>`
				+`</div>`
				+`<span><i class="layui-icon layui-icon-close a-hover" onclick="delAllReceivable()"></i></span>`
				+`</div>`
	if(data.length>0){
		$("#receivable").html(cont1+cont);
		$(".total-num").html("<span>共 ¥ "+he+"</span>");
		var laydate = layui.laydate;
		if(startDate !=$("#startDate").val()){
			$(".jiaozu-erro").html("合同开始时间必须等于合同周期首'阶段开始'日期");
		}
		if(endDate != $("#endDate").val()){
			$(".jiaozu-erro").html("合同结束时间必须等于合同周期最后'阶段终止'日期");
		}
		for(var i = 0;i < data.length;i++){
			//执行一个laydate实例
			  laydate.render({
			    elem: '#startdate'+i //指定元素
			  });
			  laydate.render({
				    elem: '#enddate'+i //指定元素
				});
		}
	}else{
		$(".jiaozu-erro").html("请添加至少一个缴租阶段");
	}				
	
	setHeight();//更新容器高度
}
function delAllReceivable(){
	var id=$("#id").val();
	parent.parent.layer.confirm('是否确定删除缴租阶段？', function(index){
    	//发送删除请求
    	$.post("/app/contractReceivable/deleteAll",{id:id},function(res){
    		parent.parent.layer.msg(res.msg);
    		parent.parent.layer.close(index);
    		$("#receivable").html("");
    		$(".total-num").html("");
    		setHeight();//更新容器高度
        },"json");
  	});
}
function delReceivable(id){
   	//发送删除请求
   	$.post("/app/contractReceivable/delete",{id:id},function(res){
   		parent.parent.layer.msg(res.msg);
   		getReceivable();
   		setHeight();//更新容器高度
   	},"json");
   	
}
function getPartition(){
	var id=$("#id").val();
	var url="/app/contractPartition/listdata";
	jQuery.ajax({
        type:"post",
        async:false,
        url:url,
        dataType:"json",
        data:{'params[contractId]':id},
        success:function(data){
        	partition(data.data);
        }
    })
}
function partition(data){
	var cont1="";
	var cont2="";
	var cont0="";
	var stationPrice="";
	var stationNumber = "";
	var roomPrice = "";
	var totalRent = "";
	for (var i = 0; i < data.length; i++) {
		if(allPartitionId.indexOf(data[i].partition.id)==-1){
			if(data[i].stationPrice){
				stationPrice = data[i].stationPrice;
			}
			if(data[i].stationNumber){
				stationNumber = data[i].stationNumber;
			}
			if(data[i].totalRent){
				totalRent = data[i].totalRent;
			}
			if(data[i].roomPrice){
				roomPrice = data[i].roomPrice;
			}
			if(data[i].partition.partitionType==1){//开放
				cont1+=`<li style="padding: 0px 30px 30px 0px;" id="`+data[i].id+`li" pId="`+data[i].partition.id+`">`
				+`<i class="layui-icon layui-icon-close" onclick=delPar("`+data[i].id+`","`+data[i].partition.id+`")></i>`
				+`<div class="layui-form-item layui-row ">`
					+`<div class="layui-col-sm2 layui-col-md2">`
				    	+`<div class="layui-btn layui-btn-normal block">`+data[i].partition.partitionName+`</div>`
				    +`</div>`
				    +`<div class="layui-col-sm5 layui-col-md5">`
				    	+`<label class="layui-form-label"><span class="must-fill">*</span>工位价格</label>`
					    +`<div class="layui-input-block">`
					    	+`<input type="text" name="kfjiage`+data[i].partition.id+`" id="kfjiage`+data[i].partition.id+`" placeholder="" autocomplete="off" onkeyup=kfjisuan("`+data[i].partition.id+`",this) class="layui-input input-dollar-moth" value="`+stationPrice+`">`
					    	+`<p class="error-info"></p>`
					    	+`</div>`
				    +`</div>`
				    +`<div class="layui-col-sm5 layui-col-md5" >`
				    	+`<label class="layui-form-label t-right"><span class="must-fill">*</span>工位数量</label>`
					    +`<div class="layui-input-block">`
					    	+`<input type="text" name="kfshuliang`+data[i].partition.id+`" id="kfshuliang`+data[i].partition.id+`" placeholder="" autocomplete="off" data-num="`+data[i].partition.ratedNumber+`" onkeyup=countCheck("`+data[i].partition.id+`","`+data[i].partition.ratedNumber+`","kf",this) class="layui-input input-num"  value="`+stationNumber+`">`
					    	+`<p class="error-info"></p>`
					    	+`</div>`
				    +`</div>`
				+`</div>`
				+`<div class="rent-box border-none">`
					+`<div class="total-rent">`
						+`<div class="">`
							+`<label class="layui-form-label">总租金:</label>`
							+`<div class="layui-input-inline">`
						    	+`<input type="text" name="kfzujin" id="kfzujin`+data[i].partition.id+`" placeholder="" autocomplete="off" class="layui-input input-dollar-moth" value="`+totalRent+`">`
						    +`</div>`
						+`</div>`
					+`</div>`
					+`<div class="allot-desk mag-top-20 clearfix hide" style="padding-left:15px;">`
					+`<div style="margin-right: 22px; color: rgb(50, 50, 50);display:inline-block" class="f-left">选择工位:</div>`
					+`<div class="total-rent layui-form-item " style="display:inline-block">`
						
						+`<div class="allot-type" style="margin-top: 10px;">`
						    +`<input type="radio" value="1" name="radio-0" id="manual-radio0" title="手动分配工位" style="display:inline-block" data-id="`+data[i].partition.id+`">`
						    +`<label for="manual-radio0" style="margin-left: 10px;">手动分配工位</label>`
						    +`<div  style="margin-left: 10px;display:none" class="yifenpei" >已分配工位号:<span class="stationName"></span><input type="text" value="" class="fkStation" hidden="hidden"></div>`
						    +`<a class="layui-btn layui-btn-xs  layui-btn-disabled xiugaiBtn"  style="margin-left:20px;" onclick="xiugai(this)">修改</a>`
						+`</div>`
					+`</div>`
				  +`</div>`
			+`</div>`
		+`</li>`;
			
				allPartitionId+=data[i].partition.id+",";
				$("#kaifang").show();
				form.render('radio');
			}else if(data[i].partition.partitionType==2){//封闭
				cont2+=`<li style="padding: 0px 30px 30px 0px;" id="`+data[i].id+`li" pId="`+data[i].partition.id+`">`
				+`<i class="layui-icon layui-icon-close" onclick=delPar("`+data[i].id+`","`+data[i].partition.id+`")></i>`
				+`<div class="layui-form-item layui-row ">`
					+`<div class="layui-col-sm2 layui-col-md2">`
				    	+`<div class="layui-btn layui-btn-normal block">`+data[i].partition.partitionName+`</div>`
				    +`</div>`
				    +`<div class="layui-col-sm3 layui-col-md3">`
				    	+`<label class="layui-form-label"><span class="must-fill">*</span>工位价格</label>`
					    +`<div class="layui-input-block">`
					    	+`<input type="text" name="fbjiage`+data[i].partition.id+`" id="fbjiage`+data[i].partition.id+`" placeholder="" autocomplete="off" onkeyup=fbjisuan("`+data[i].partition.id+`",this) class="layui-input input-dollar-moth"  value="`+stationPrice+`">`
					    	+`<p class="error-info"></p>`
					    	+`</div>`
					    +`</div>`
				 +`<div class="layui-col-sm3 layui-col-md3" >`
					    +`<label class="layui-form-label t-right"><span class="must-fill">*</span>工位数量</label>`
					    +`<div class="layui-input-block">`
					    +`<input type="text" name="fbshuliang`+data[i].partition.id+`" id="fbshuliang`+data[i].partition.id+`" placeholder="" autocomplete="off" value="`+data[i].partition.ratedNumber+`" data-num="`+data[i].partition.ratedNumber+`" onkeyup=countCheck("`+data[i].partition.id+`","`+data[i].partition.ratedNumber+`","fb",this) class="layui-input input-num" value="`+stationNumber+`">`
					    +`<p class="error-info"></p>`
					    +`</div>`
				+`</div>`
				+`<div class="layui-col-sm4 layui-col-md4" >`
					    +`<label class="layui-form-label t-right"><span class="must-fill">*</span>房间价格</label>`
					    +`<div class="layui-input-block">`
					    +`<input type="text" name="fbfangjian`+data[i].partition.id+`" id="fbfangjian`+data[i].partition.id+`" placeholder="" autocomplete="off" class="layui-input input-dollar-moth" value="`+roomPrice+`">`
				+`</div>`
				+`</div>`
				+`</div>`
				+`<div class="rent-box border-none">`
					+`<div class="total-rent">`
					+`<div class="total-rent">`
					    +`<label class="layui-form-label">总租金:</label>`
					    +`<div class="layui-input-inline">`
					    +`<input type="text" name="fbzujin" id="fbzujin`+data[i].partition.id+`" placeholder="" autocomplete="off" class="layui-input input-dollar-moth" value="`+totalRent+`">`
					    +`</div>`
					+`</div>`
					+`</div>`
					+`</div>`
				+`</li>`;
				allPartitionId+=data[i].partition.id+",";
				$("#fengbi").show();
			}
		}
	}
	$("#kaifangCont").append(cont1);
	$("#fengbiCont").append(cont2);
	for (var i = 0; i < data.length; i++) {
		if(data[i].partition.partitionType==1 && data[i].stationNumber != null && data[i].stationNumber !="" && data[i].stationNumber > 0){//开放
			$("#kfshuliang"+data[i].partition.id).parents('li').children().children(".allot-desk").addClass("show").removeClass('hide')
			getStation(data[i].partition.id);
		}
	}
	if($("#kaifangCont li").length > 0){
		$("#kaifang").show();
	}
	if($("#fengbiCont li").length > 0){
		$("#fengbi").show();
	}
	setHeight();//更新容器高度
}
var deposit=0;
function kfjisuan(id,obj){
	var kfjiage=$("#kfjiage"+id).val();
	var kfshuliang=$("#kfshuliang"+id).val();
	if(kfshuliang>0){
		$("#kfshuliang"+id).parents('li').children().children(".allot-desk").addClass("show").removeClass('hide')
	}else{
		$("#kfshuliang"+id).parents('li').children().children(".allot-desk").addClass("hide").removeClass('show')
	}
	if(checkNum(kfjiage)){
		
		$("#kfzujin"+id).val((kfjiage*kfshuliang).toFixed(2));
		yjSum();
	}else{
		$(this).next().html("请输入正确的价格, 小数点保留2位");
	}
	
	
}
function fbjisuan(id){
	var fbjiage=$("#fbjiage"+id).val();
	if(checkNum(fbjiage)){
		var fbshuliang=$("#fbshuliang"+id).val();
		
		$("#fbfangjian"+id).val((fbjiage*fbshuliang).toFixed(2));
		$("#fbzujin"+id).val((fbjiage*fbshuliang).toFixed(2));
		yjSum();
	}else{
		$(this).next().html("请输入正确的价格, 小数点保留2位");
	}
	
}
function yjSum(){
	var deposit=0; 
	$("input[name='kfzujin']").each(function(){
		deposit+=$(this).val()*1;
	});
	$("input[name='fbzujin']").each(function(){
		deposit+=$(this).val()*1;
	});
	
	var yf = $("select[name='interest']").val();
	$("#deposit").val(deposit*yf);
}

//个数验证
function countCheck(id,count,flag,obj){
	$(".error-info").html("");
	if(flag == "fb"){
		var fbshuliang=$("#fbshuliang"+id).val();
		fbjisuan(id,obj);
		if(fbshuliang > count){
			$(obj).next().html("所选分区工位数不足");
		}
	}else{
		var kfshuliang=$("#kfshuliang"+id).val();
		kfjisuan(id,obj);
		if(kfshuliang > count){
			$(obj).next().html("所选分区工位数不足");
		}
	}
}

function delPar(id,partitionId){
	 allPartitionId=allPartitionId.replace(partitionId+",","");
	
	var contractId = $("#id").val();
	$("#"+id+"li").remove();
	//发送删除请求
	$.post("/app/contractPartition/delete",{id:id,contractId:contractId},function(res){
    },"json");
	if($("#kaifangCont li").length > 0){
		$("#kaifang").show();
	}else{
		$("#kaifang").hide();
	}
	if($("#fengbiCont li").length > 0){
		$("#fengbi").show();
	}else{
		$("#fengbi").hide();
	}
	//更新id
	setHeight();//更新容器高度
}
function getnow(){
	var myDate = new Date();
	//获取当前年
	var year=myDate.getFullYear();
	//获取当前月
	var month=myDate.getMonth()+1;
	//获取当前日
	var date=myDate.getDate();
	var now=year+'-'+p(month)+"-"+p(date);
	$("#signTime").val(now);
	return {
			year:year,
			month: month,
			date: date
	};
}
function p(s) {
    return s < 10 ? '0' + s: s;
}
//计算应收总金额
function getAmountSum(){
	var price = 0;
	var list = $("input[name='price_min']");
	for(var i = 0;i < list.length;i++){
		price += parseFloat($(list[i]).val());
	}
	$(".total-num span").text("共 ￥："+price);
}
//重新渲染表单
function renderForm(){
    layui.use('form', function(){
       var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()
       form.render();
    });
}
//验证数字或者小数
function checkNum(num){
	var reg=/^\d+(\.\d{0,2})?$/;
	if(reg.test(num)){
		return true;
	}else{
		return false;
	}
}
</script>
</html>